package ps.server.trigger;

import java.awt.Color;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ps.net.TriggerEventContent;


public class TriggerEntry {

//    public static final int NO_CATEGORY = 0;
    public static final String NO_CATEGORY = "";
    public static final String NO_SOUND = "<kein>";
    
    int id = 0;
    String title = "";
    boolean active = true;
//    int category = NO_CATEGORY;
    String category = NO_CATEGORY;
    
    String regex = "";
    int quantity = 1;
    int ignoreTimer = 20;
    
    boolean serverMsgActive = false;
    String serverMsg = "";
    int serverMsgSize = 12;
    Color serverMsgColor = Color.WHITE;
    
    boolean soundActive = false;
    String sound = NO_SOUND;
    
    boolean timerActive = false;
    boolean timerShow1 = true;
    boolean timerShow2 = false;
    int timerPeriod = 42;
    int timerWarning = 8;
    String timerWarningMsg = "";
    int timerWarningMsgSize = 12;
    Color timerWarningMsgColor = Color.WHITE;
    String timerWarningSound = NO_SOUND;
    int timerRemove = 15;
    
    Pattern pattern;
    long firstTriggerTime = 0;
    int triggerCount = 0;
    
    String solvedServerMessage = "";
    String solvedTimerWarningMessage = "";
    
    HashSet<String> attrStrings = new HashSet<String>();
    
    @Override
    public String toString() {
        return title;
    }
    
    @Override
    public boolean equals(Object o) {
        if (o != null && o instanceof TriggerEntry) {
            if (title.equalsIgnoreCase(o.toString())) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public Object clone() {
        TriggerEntry ret = new TriggerEntry();
        
        ret.id = getId();
        ret.title = getTitle();
        ret.active = isActive();
        ret.category = getCategory();
        
        ret.regex = getRegex();
        ret.quantity = getQuantity();
        ret.ignoreTimer = getIgnoreTimer();
        
        ret.serverMsgActive = isServerMsgActive();
        ret.serverMsg = getServerMsg();
        ret.serverMsgSize = getServerMsgSize();
        ret.serverMsgColor = getServerMsgColor();
        
        ret.soundActive = isSoundActive();
        ret.sound = getSound();
        
        ret.timerActive = isTimerActive();
        ret.timerShow1 = isTimerShow1();
        ret.timerShow2 = isTimerShow2();
        ret.timerPeriod = getTimerPeriod();
        ret.timerWarning = getTimerWarning();
        ret.timerWarningMsg = getTimerWarningMsg();
        ret.timerWarningMsgSize = getTimerWarningMsgSize();
        ret.timerWarningMsgColor = getTimerWarningMsgColor();
        ret.timerWarningSound = getTimerWarningSound();
        ret.timerRemove = getTimerRemove();
        
        return ret;
    }
    
    public void solveServerMsg(TriggerEventContent triggerEvtCont) {
        solvedServerMessage = solveMessage(getServerMsg(), triggerEvtCont);
    }
    
    public void solveTimerWarningMessage(TriggerEventContent triggerEvtCont) {
        solvedTimerWarningMessage = solveMessage(getTimerWarningMsg(), triggerEvtCont);
    }

    private static final String VAR_USER = "%u"; // user
    private static final String VAR_SPELL = "%s"; // spell
    private static final String VAR_ATTACKER = "%a"; // attacker
    private static final String VAR_TARGET = "%t"; // target
    private static final String VAR_TARGETS_GROUP = "%g"; // targets group
    private static final Pattern CUSTOM_VAR_PATTERN = Pattern.compile("(" + VAR_USER + "|" + VAR_SPELL + "|" + VAR_ATTACKER + "|" + VAR_TARGET + "|" + VAR_TARGETS_GROUP + ")");
    
    private String solveMessage(String msg, TriggerEventContent triggerEvtCont) {
        if (msg.length() > 0) {
            String[] triggerAttr = triggerEvtCont.getAttrStr().split(";");
            StringBuilder solvedMsg = new StringBuilder();
            int newContentIndex = 0;
            Matcher matcher = CUSTOM_VAR_PATTERN.matcher(msg);
            while (matcher.find()) {
                solvedMsg.append(msg.substring(newContentIndex, matcher.start()));
                newContentIndex = matcher.end();
                if (msg.substring(matcher.start()).startsWith(VAR_USER)) {
                    solvedMsg.append(triggerEvtCont.getSender());
                } else if (triggerAttr.length >= 4) {
                    if (msg.substring(matcher.start()).startsWith(VAR_SPELL)) {
                        solvedMsg.append(triggerAttr[0]);
                    } else if (msg.substring(matcher.start()).startsWith(VAR_ATTACKER)) {
                        solvedMsg.append(triggerAttr[1]);
                    } else if (msg.substring(matcher.start()).startsWith(VAR_TARGET)) {
                        solvedMsg.append(triggerAttr[2]);
                    } else if (msg.substring(matcher.start()).startsWith(VAR_TARGETS_GROUP)) {
                        solvedMsg.append(triggerAttr[3]);
                    }
                }
            }
            solvedMsg.append(msg.substring(newContentIndex));
            
            return solvedMsg.toString();
        }
        return "";
    }
    
    public Pattern getPattern() {
        if (pattern == null) {
            pattern = Pattern.compile(regex);
        }
        return pattern;
    }
    
    public Matcher getMatcher(String str) {
        return getPattern().matcher(str);
    }

    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getRegex() {
        return regex;
    }

    public void setRegex(String regex) {
        pattern = null;
        this.regex = regex;
    }
    
    public String getServerMsg() {
        return serverMsg;
    }

    public void setServerMsg(String serverMsg) {
        this.serverMsg = serverMsg;
    }
    
    public int getServerMsgSize() {
        return serverMsgSize;
    }

    
    public void setServerMsgSize(int serverMsgSize) {
        this.serverMsgSize = serverMsgSize;
    }

    
    public Color getServerMsgColor() {
        return serverMsgColor;
    }

    
    public void setServerMsgColor(Color serverMsgColor) {
        this.serverMsgColor = serverMsgColor;
    }

    
    public int getIgnoreTimer() {
        return ignoreTimer;
    }

    
    public void setIgnoreTimer(int ignoreTimer) {
        this.ignoreTimer = ignoreTimer;
    }
    
    public int getTimerPeriod() {
        return timerPeriod;
    }

    
    public void setTimerPeriod(int timerPeriod) {
        this.timerPeriod = timerPeriod;
    }

    
    public int getTimerWarning() {
        return timerWarning;
    }

    
    public void setTimerWarning(int timerWarning) {
        this.timerWarning = timerWarning;
    }

    
    public String getTimerWarningMsg() {
        return timerWarningMsg;
    }

    
    public void setTimerWarningMsg(String timerWarningMsg) {
        this.timerWarningMsg = timerWarningMsg;
    }

    
    public int getTimerWarningMsgSize() {
        return timerWarningMsgSize;
    }

    
    public void setTimerWarningMsgSize(int timerWarningMsgSize) {
        this.timerWarningMsgSize = timerWarningMsgSize;
    }

    
    public Color getTimerWarningMsgColor() {
        return timerWarningMsgColor;
    }

    
    public void setTimerWarningMsgColor(Color timerWarningMsgColor) {
        this.timerWarningMsgColor = timerWarningMsgColor;
    }

    
    public String getTimerWarningSound() {
        return timerWarningSound;
    }

    
    public void setTimerWarningSound(String timerWarningMsgSound) {
        this.timerWarningSound = timerWarningMsgSound;
    }

    
    public int getTimerRemove() {
        return timerRemove;
    }

    
    public void setTimerRemove(int timerRemove) {
        this.timerRemove = timerRemove;
    }

    
    public boolean isActive() {
        return active;
    }

    
    public void setActive(boolean active) {
        this.active = active;
    }

    
    public int getQuantity() {
        return quantity;
    }

    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    
    public boolean isServerMsgActive() {
        return serverMsgActive;
    }

    
    public void setServerMsgActive(boolean serverMsgActive) {
        this.serverMsgActive = serverMsgActive;
    }

    
    public boolean isSoundActive() {
        return soundActive;
    }

    
    public void setSoundActive(boolean soundActive) {
        this.soundActive = soundActive;
    }

    public String getSound() {
        return sound;
    }
    
    public void setSound(String sound) {
        this.sound = sound;
    }

    
    public void setTimerActive(boolean timerActive) {
        this.timerActive = timerActive;
    }
    
    public boolean isTimerActive() {
        return timerActive;
    }
    
//    public int getCategory() {
//        return category;
//    }
//
//    
//    public void setCategory(int category) {
//        this.category = category;
//    }

    
    public boolean isTimerShow1() {
        return timerShow1;
    }

    
    public void setTimerShow1(boolean timerShow1) {
        this.timerShow1 = timerShow1;
    }

    
    public boolean isTimerShow2() {
        return timerShow2;
    }

    
    public void setTimerShow2(boolean timerShow2) {
        this.timerShow2 = timerShow2;
    }

    
    public long getFirstTriggerTime() {
        return firstTriggerTime;
    }

    
    public void setFirstTriggerTime(long lastTriggerTime) {
        this.firstTriggerTime = lastTriggerTime;
    }

    
    public int getTriggerCount() {
        return triggerCount;
    }

    
    public void setTriggerCount(int triggerCount) {
        this.triggerCount = triggerCount;
    }
    
    public void increaseTriggerCount() {
        triggerCount++;
    }

    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }

    
    public String getCategory() {
        return category;
    }

    
    public void setCategory(String category) {
        this.category = category;
    }
    
    public void clearAttrStrings() {
        attrStrings.clear();
    }
    
    public void addAttrString(String attrStr) {
        attrStrings.add(attrStr);
    }
    
    public boolean containsAttrString(String attrStr) {
        return attrStrings.contains(attrStr);
    }

    public String getSolvedServerMsg() {
        return solvedServerMessage;
    }
    
    public String getSolvedTimerWarningMessage() {
        return solvedTimerWarningMessage;
    }
    
}
